<script setup lang="ts">
import type { FormInstance } from '@arco-design/web-vue'
import { Message } from '@arco-design/web-vue'
import { FinAccountApi } from '@/apis'
import { useForm } from '@/hooks'
import * as Regexp from '@/utils/regexp'
import ProFormPanel from '@/components/ProForm/ProFormPanel.vue'

const emit = defineEmits<{
  (e: 'change'): void
}>()

const visible = ref(false)
const loading = ref(false)
const readonly = ref(true)

const dataId = ref('')
const dataDetail = ref<FinAccountApi.FinAccountDetailResp>()

const isCreateMode = computed(() => dataId.value === '')
const title = computed(() => (isCreateMode.value ? '创建财务账户' : '修改财务账户'))

const formRef = ref<FormInstance>()

const { form, resetForm } = useForm({
  name: null,
  account: null,
  status: 1,
  remark: null,
})

const rules: FormInstance['rules'] = {
  name: [{ required: true, match: Regexp.Name, message: '名称只能含有中文、数字、英文、下划线、短横线,长度为2-32位' }],
}

// 查询详情
const getDataDetail = async () => {
  dataDetail.value = await FinAccountApi.getDetail(dataId.value)
  for (const key in form) {
    form[key] = dataDetail.value?.[key]
  }
  loading.value = false
}

// 打开详情
const open = async (id: string) => {
  dataId.value = id
  loading.value = true
  visible.value = true
  readonly.value = true
  getDataDetail()
}

// 打开新增
const create = async () => {
  dataId.value = ''
  loading.value = false
  visible.value = true
  readonly.value = false
}

// 保存
const onSave = async () => {
  try {
    const isInvalid = await formRef.value?.validate()
    if (isInvalid) {
      return false
    }
    if (isCreateMode.value) {
      await FinAccountApi.create(form)
      Message.success('添加成功')
    } else {
      await FinAccountApi.update(form, dataId.value)
      Message.success('修改成功')
    }
    emit('change')
    visible.value = false
  } catch {
    return false
  }
}

// 取消
const onCancel = () => {
  readonly.value = true
  for (const key in form) {
    form[key] = dataDetail.value?.[key]
  }
  formRef.value?.clearValidate()
}

// 编辑
const onEdit = () => {
  readonly.value = false
}

// 重置
const reset = () => {
  formRef.value?.resetFields()
  resetForm()
}

defineExpose({ open, create })
</script>

<template>
  <ProFormPanel
    v-model:visible="visible"
    mode="drawer"
    :title="title"
    :loading="loading"
    :footer="isCreateMode"
    @confirm="onSave"
    @close="reset"
  >
    <a-form ref="formRef" layout="vertical" :model="form" :rules :readonly>
      <a-card title="基础信息" class="section">
        <template #extra>
          <ReadonlyEditButton
            v-if="!isCreateMode"
            :readonly="readonly"
            :deleteable="false"
            @edit="onEdit"
            @cancel="onCancel"
            @save="onSave"
          />
        </template>
      </a-card>
      <a-row :gutter="28">
        <readonly-form-item label="名称" field="name">
          <a-input v-model.trim="form.name" placeholder="请输入名称" />
        </readonly-form-item>
        <readonly-form-item label="账户" field="account">
          <a-input v-model="form.account" placeholder="请输入账户" />
        </readonly-form-item>
        <readonly-form-item label="状态" field="status">
          <DictSelect v-model="form.status" code="CommonStatusEnum" type="switch" />
          <template #readonly>
            <CellStatus :status="dataDetail?.status" />
          </template>
        </readonly-form-item>
        <readonly-form-item label="备注" field="remark" :span="24">
          <a-textarea v-model.trim="form.remark" placeholder="请输入备注" :max-length="255" show-word-limit />
        </readonly-form-item>
      </a-row>
    </a-form>
  </ProFormPanel>
</template>

<style lang="less" scoped></style>
